package com.open.capacity.user.controller;

import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.exception.service.ServiceException;
import com.open.capacity.common.model.Notice;
import com.open.capacity.common.web.PageResult;
import com.open.capacity.common.web.Result;
import com.open.capacity.user.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/notice")
@Api(tags = "通知模块")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/getSelfPublishNotice")
    @ApiOperation(value = "获取自己发布的通知")
    public PageResult getSelfPublishNotice(@RequestParam Map map) {
        try {
            return noticeService.getSelfPublishNotice(map);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }

    }

    @GetMapping("/getNoReadNoticeCount")
    @ApiOperation(value = "获取未读通知数")
    public Result getNoReadNoticeCount(@RequestParam Long userId) {
        try {
            return noticeService.getNoReadNoticeCount(userId);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }

    @GetMapping("/getReceivedAndMarkNoReadNotice")
    @ApiOperation(value = "获取自己所收到的通知并标记未读通知")
    public PageResult getReceivedAndMarkNoReadNotice(@RequestParam Map map) {
        try {
            return noticeService.getReceivedAndMarkNoReadNotice(map);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }
    }

    @GetMapping("/readNotice")
    @ApiOperation(value = "阅读通知")
    public Result readNotice(@RequestParam("id") Integer id, @RequestParam("userId") String userId) {
        try {
            Long usersId = Long.valueOf(userId);
            return noticeService.readNotice(id, usersId);
        }catch (ServiceException e) {
            throw new ControllerException(e);
        }
    }

    @PostMapping("/publishOrUpdateNotice")
    @ApiOperation(value = "发布通知")
    public Result publishOrUpdateNotice(@RequestBody Notice notice) {
        try {
            return noticeService.publishOrUpdateNotice(notice);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }

    }

    @GetMapping("/deleteNotice")
    @ApiOperation(value = "删除通知")
    public Result deleteNotice(@RequestParam Integer id) {
        try {
            return noticeService.deleteNotice(id);
        }catch (ServiceException e){
            throw new ControllerException(e);
        }

    }
}
